package com.yuxin.site;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * @author zehui.zeng
 * @date 13-4-14 下午2:54
 */
public class ConfigService {
    private static Map permissionConfig = null;// 权限配置对象

    /***
     * 获取微站权限配置
     * @return
     */
    public static Map getWZPermConfig(){
        if(permissionConfig != null){
            return permissionConfig;
        }
        Gson gson = new Gson();
        permissionConfig = gson.fromJson(getConfigFileString("permission.json"),Map.class);
        return permissionConfig;
    }

    /***
     * 获取配置文件字符串
     * @param fileName
     * @return
     */
    private static String getConfigFileString(String fileName){
        StringBuffer fileString = new StringBuffer();
        InputStream inputStream = ConfigService.class.getClassLoader().getResourceAsStream(fileName);
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            String str;
            while ((str = in.readLine()) != null){
                fileString.append(str).append("\n");
            }
            in.close();
        }catch (IOException e){
            e.getStackTrace();
        }
        // 去除注释
        //String configStr = fileString.toString().replaceAll("//.*?\n|/\\*.*?\\*/", "");
        String configStr = fileString.toString().replaceAll("//.*?\n", "");
        return configStr;
    }

   public static void main(String[] str){
       //System.out.println(getConfigFileString("templ.json"));

   }
}
